const WebSocket = require('ws');

const server = new WebSocket.Server({ port: 9000 });

server.on('connection', (ws) => {
    console.log('New client connected');

    ws.on('message', (message) => {
        console.log(`Received: ${message}`);
        const video_url = '${message}';
// 下载函数
        function download(url,name){
            const a = document.createElement("a");
            a.href = url;
            a.download = name;
            a.click();
            a.remove();
        }

        download(video_url, ('test_video'+Date.now()))

        // 广播收到的消息给所有客户端
        server.clients.forEach(client => {
            if (client !== ws && client.readyState === WebSocket.OPEN) {
                client.send(message);
            }
        });
    });

    ws.on('close', () => {
        console.log('Client disconnected');
    });

    ws.send('Welcome to WebSocket server!');
});

console.log('WebSocket server is running on ws://172.16.76.203:9000');
